package com.jk.controller.account;

import com.jk.dto.JsonResult;
import com.jk.entity.account.AccPermission;
import com.jk.service.account.AccPermissionService;
import com.jk.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * Created by yanghuan on 15-8-31.
 */
@RequestMapping("/account/permission")
@Controller
public class PermissionController {
    @Resource
    private AccPermissionService permissionService;


    //跳转到权限管理页面
    @RequestMapping("/page")
    public String managePermission() {
        return "account/permission";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public String getList() {
        try {
            return JsonUtil.toString(permissionService.getList());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new ArrayList<AccPermission>());
        }
    }

    //创建权限
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String createPermission(AccPermission accPermission) {
        try {
            permissionService.insert(accPermission);
            return JsonUtil.toString(new JsonResult(200, "保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "保存失败"));
        }
    }

    //删除权限
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public String delPermission(String id) {
        try {
            permissionService.delete(id);
            return JsonUtil.toString(new JsonResult(200, "删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "删除失败"));
        }
    }

}
